/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.viweb.storemanager.report;

import com.viweb.storemanager.entity.Category;
import com.viweb.storemanager.entity.Customer;
import com.viweb.storemanager.entity.Employee;
import com.viweb.storemanager.entity.InfoConfig;
import com.viweb.storemanager.entity.Product;
import com.viweb.storemanager.entity.Vendor;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JPanel;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.view.JRViewer;

/**
 *
 * @author Wind
 */
public class ReportManager {

    InfoConfig info;

    public ReportManager(InfoConfig info) {
        this.info = info;
    }
    private String _error;

    /**
     * Get input stream of report file
     * @param reportName report file name
     * @return InputStream
     */
    protected InputStream getInputStream(String reportName) {
        URL url = getClass().getResource("/com/viweb/storemanager/report/jasper/" + reportName);
        InputStream input = null;

        try {
            input = url.openStream();
            return input;
        } catch (IOException ex) {
            setError("lỗi truy cập report file");
            return null;
        }
    }

    /**
     * Get JPanel viewer of report
     * @param cusList list of customer
     * @return JPanel
     */
    public JPanel getCustomerDetailsViewer(ArrayList<Customer> cusList) {
        JPanel viewer = null;
        try {

            /**
             * get parameter of report
             */
            HashMap parameterMap = new HashMap();
            parameterMap.put("SHOPNAME", info.getName());
            parameterMap.put("SHOPADDRESS", info.getAddress());
            parameterMap.put("SHOPPHONE", info.getPhone());
            parameterMap.put("REPORTNAME", "DANH SÁCH KHÁCH HÀNG");
            parameterMap.put("ID", "SỐ ID");
            parameterMap.put("NAME", "HỌ TÊN");
            parameterMap.put("TYPE", "LOẠI KHÁCH HÀNG");
            parameterMap.put("ADDRESS", "ĐỊA CHỈ");
            parameterMap.put("EMAIL", "EMAIL");
            parameterMap.put("PHONE", "SỐ ĐIỆN THOẠI");
            parameterMap.put("BANK", "NGÂN HÀNG");
            parameterMap.put("ACCOUNTNUMBER", "SỐ TÀI KHOẢN");

            /**
             * get data field of report
             */
            ArrayList reportRows = new ArrayList();
            for (Customer cus : cusList) {
                HashMap row = new HashMap();


                row.put("ID", cus.getId());
                row.put("NAME", cus.getName());
                row.put("TYPE", cus.getType().getName());
                row.put("ADDRESS", cus.getAddress());
                row.put("EMAIL", cus.getEmail());
                row.put("PHONE", cus.getPhone());
                row.put("BANK", cus.getBank());
                row.put("ACCOUNTNUMBER", cus.getAccountNumber());

                reportRows.add(row);
            }

            /**
             * get JasperPrint object
             */
            JasperPrint jasperPrint = JasperFillManager.fillReport(
                    getInputStream("customer.jasper"),
                    parameterMap,
                    new JRMapCollectionDataSource(reportRows));

            //create JPanel
            viewer = new JRViewer(jasperPrint);

        } catch (Exception ex) {
            setError(ex.getMessage());
        }

        return viewer;
    }

    /**
     * Get JPanel viewer of report
     * @param empList list of employee
     * @return JPanel
     */
    public JPanel getEmployeeDetailsViewer(ArrayList<Employee> empList) {
        JPanel viewer = null;
        try {
            HashMap parameterMap = new HashMap();
            parameterMap.put("SHOPNAME", info.getName());
            parameterMap.put("SHOPADDRESS", info.getAddress());
            parameterMap.put("SHOPPHONE", info.getPhone());
            parameterMap.put("REPORTNAME", "DANH SÁCH NHÂN VIÊN");
            parameterMap.put("ID", "SỐ ID");
            parameterMap.put("NAME", "HỌ TÊN");
            parameterMap.put("ADDRESS", "ĐỊA CHỈ");
            parameterMap.put("PHONE", "SĐT");
            parameterMap.put("STARTDATE", "NGÀY VÀO LÀM");
            parameterMap.put("ACCOUNT", "TÊN ĐĂNG NHẬP");

            ArrayList reportRows = new ArrayList();
            for (Employee emp : empList) {
                HashMap row = new HashMap();

                row.put("ID", emp.getId());
                row.put("NAME", emp.getName());
                row.put("ADDRESS", emp.getAddress());
                row.put("PHONE", emp.getPhone());
                row.put("STARTDATE", new SimpleDateFormat("dd/MM/yyyy").format(emp.getStartdate()));
                row.put("ACCOUNT", emp.getUsername());

                reportRows.add(row);
            }

            JasperPrint jasperPrint = JasperFillManager.fillReport(
                    getInputStream("employee.jasper"),
                    parameterMap,
                    new JRMapCollectionDataSource(reportRows));

            viewer = new JRViewer(jasperPrint);
        } catch (Exception ex) {
            setError("lỗi tạo report");
        }

        return viewer;
    }

    /**
     * Get JPanel viewer of report
     * @param vendorList list of vendor
     * @return JPanel
     */
    public JPanel getVendorDetailsViewer(ArrayList<Vendor> vendorList) {
        JPanel viewer = null;
        try {
            HashMap parameterMap = new HashMap();
            parameterMap.put("SHOPNAME", info.getName());
            parameterMap.put("SHOPADDRESS", info.getAddress());
            parameterMap.put("SHOPPHONE", info.getPhone());
            parameterMap.put("REPORTNAME", "DANH SÁCH NHÀ CUNG CẤP");
            parameterMap.put("ID", "ID");
            parameterMap.put("NAME", "TÊN NCC");
            parameterMap.put("ADDRESS", "ĐỊA CHỈ");
            parameterMap.put("PHONE", "SĐT");
            parameterMap.put("FAX", "FAX");
            parameterMap.put("EMAIL", "EMAIL");

            ArrayList reportRows = new ArrayList();
            for (Vendor vendor : vendorList) {
                HashMap row = new HashMap();

                row.put("ID", vendor.getId());
                row.put("NAME", vendor.getName());
                row.put("ADDRESS", vendor.getAddress());
                row.put("PHONE", vendor.getPhone());
                row.put("FAX", vendor.getFax());
                row.put("EMAIL", vendor.getEmail());

                reportRows.add(row);
            }

            JasperPrint jasperPrint = JasperFillManager.fillReport(
                    getInputStream("vendor.jasper"),
                    parameterMap,
                    new JRMapCollectionDataSource(reportRows));

            viewer = new JRViewer(jasperPrint);
        } catch (Exception ex) {
            setError("lỗi tạo report");
        }
        return viewer;
    }

    /**
     * Get JPanel viewer of report
     * @param productList list of product
     * @param categoryList list of category
     * @return
     */
    public JPanel getProductDetailsViewer(ArrayList<Product> productList, ArrayList<Category> categoryList) {
        JPanel viewer = null;
        try {
            HashMap parameterMap = new HashMap();
            parameterMap.put("SHOPNAME", info.getName());
            parameterMap.put("SHOPADDRESS", info.getAddress());
            parameterMap.put("SHOPPHONE", info.getPhone());
            parameterMap.put("REPORTNAME", "DANH SÁCH SẢN PHẨM");
            parameterMap.put("ID", "ID");
            parameterMap.put("NAME", "TÊN HÀNG");
            parameterMap.put("CAGETORY", "LOẠI HÀNG");
            parameterMap.put("NUMBER", "SÔ LƯỢNG HIỆN CÓ");
            parameterMap.put("PRICE", "ĐƠN GIÁ");
            parameterMap.put("UNIT", "ĐƠN VỊ TÍNH");

            ArrayList reportRows = new ArrayList();
            for (Product product : productList) {
                HashMap row = new HashMap();

                String categoryname = null;
                for (Category category : categoryList) {
                    if (category.getId() == product.getCategory().getId()) {
                        categoryname = category.getName();
                        break;
                    }
                }

                if (categoryname == null) {
                    setError("không tìm thấy loại hàng phù hợp với sản phẩm");
                    return null;
                }

                row.put("ID", product.getId());
                row.put("NAME", product.getName());
                row.put("CAGETORY", categoryname);
                row.put("NUMBER", product.getNumber());
                row.put("PRICE", product.getPrice());
                row.put("UNIT", product.getUnit());

                reportRows.add(row);
            }

            JasperPrint jasperPrint = JasperFillManager.fillReport(
                    getInputStream("product.jasper"),
                    parameterMap,
                    new JRMapCollectionDataSource(reportRows));

            viewer = new JRViewer(jasperPrint);
        } catch (Exception ex) {
            setError("lỗi tạo report");
        }
        return viewer;
    }

    /**
     * Get JPanel viewer of report
     * @param typeList list of Customer Type
     * @return JPanel
     */
    public JPanel getTypeProductDetailsViewer(ArrayList<Category> typeList) {
        JPanel viewer = null;
        try {
            HashMap parameterMap = new HashMap();
            parameterMap.put("SHOPNAME", info.getName());
            parameterMap.put("SHOPADDRESS", info.getAddress());
            parameterMap.put("SHOPPHONE", info.getPhone());
            parameterMap.put("REPORTNAME", "DANH SÁCH LOẠI HÀNG");
            parameterMap.put("ID", "ID");
            parameterMap.put("NAME", "TÊN LOẠI HÀNG");

            ArrayList reportRows = new ArrayList();
            for (Category type : typeList) {
                HashMap row = new HashMap();

                row.put("ID", type.getId());
                row.put("NAME", type.getName());

                reportRows.add(row);
            }

            JasperPrint jasperPrint = JasperFillManager.fillReport(
                    getInputStream("typeproduct.jasper"),
                    parameterMap,
                    new JRMapCollectionDataSource(reportRows));

            viewer = new JRViewer(jasperPrint);
        } catch (Exception ex) {
            setError("lỗi tạo report");
        }
        return viewer;
    }

    /**
     * @return the _error
     */
    public String getError() {
        return _error;
    }

    /**
     * @param error the _error to set
     */
    public void setError(String error) {
        this._error = error;
    }
}
